import Vue from 'vue'
import VueRouter from 'vue-router'

import Reg from '@/views/Login/Reg/reg.vue'
import Login from '@/views/Login/login.vue'
import Main from '../views/Login/Main/Main.vue'
// import Home from '@/views/Login/home/home.vue'
// import UserPhoto from '@/views/userInfo/userPhoto'
// import UserInfo from '@/views/userInfo/userInfo'
import store from '@/store'
import UserPwd from '@/views/userInfo/UserPwd'
import cate from '@/views/article/cate'
import List from '@/views/article/list'
// 把下面的代码粘贴到路由模块中对应的位置，即可防止路由报错的问题
const originalPush = VueRouter.prototype.push
VueRouter.prototype.push = function push (location) {
  return originalPush.call(this, location).catch(err => err)
}
Vue.use(VueRouter)

const routes = [
  // 注册页面
  {
    path: '/reg',
    component: Reg
  },
  // 登录页面
  {
    path: '/login',
    component: Login
  },
  // 主页
  {
    path: '/',
    component: Main,
    redirect: '/home',
    // 子路由
    children: [
      // 首页
      {
        path: '/home',
        // component: Home
        // 懒加载语法：() => import('组件路径')
        // 谁会调用函数？访问当前页面
        component: () => import('@/views/Login/home/home.vue')
      },
      // 基本资料
      {
        path: '/user-info',
        // component: UserInfo
        component: () => import('@/views/userInfo/userPhoto')
      },
      // 上传头像
      {
        path: '/user-avatar',
        // component: UserPhoto
        component: () => import('@/views/userInfo/userInfo')
      },
      // 重置密码
      {
        path: '/user-pwd',
        component: UserPwd
      },
      {
        path: '/art-cate',
        component: cate
      },
      {
        path: 'art-list',
        component: List
      }
    ]

  }
]

const router = new VueRouter({
  routes
})
// 失去token   禁止访问 登录注册除外
router.beforeEach((to, from, next) => {
  const vips = ['/login', '/reg']
  if (vips.includes(to.path)) {
    next()
  } else {
    if (store.state.user.token) {
      next()
    } else {
      next('/reg')
    }
  }
})
export default router
